import axios, { AxiosError } from 'axios';

const API_BASE_URL = '/api';
const PROGRESS_URL=API_BASE_URL+"/progress"


export default{
  async sendRequest(url,method,data=null)  {
    try {
      const response = await axios({
        url: API_BASE_URL+url,
        method: method.toLowerCase(),
        data: data,
        headers: {
          'Content-Type': 'application/json'
        }
      });
      return response.data;
    } catch (error) {
      console.error("请求出错");
      console.log(error);
      
    }
  }
}

export const progressService={
  
  async getLearnGoal(userId){
    try {
      const params={
        userId:userId
      }

      const response = await axios.get(PROGRESS_URL+"/learnGoal",
        {
          params:params
        });
      if(response.data.code!=1){
        throw new Error("请求失败");
      }

      return response.data;
    } catch (error) {
      // throw this.handleError(error);
      console.log(error);
      
    }
  },

  async getDashboard(userId){
    try {
      const params={
        userId:userId
      }

      const response = await axios.get(PROGRESS_URL+"/dashboard",
        {
          params:params
        });
      if(response.data.code!=1){
        throw new Error("请求失败");
      }

      return response.data;
    } catch (error) {
      console.log(error);
    }
  }
}